package com.incar.chuxing.activity.personal;

import android.content.Context;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.incar.chuxing.R;
import com.incar.chuxing.api.ApiStore;
import com.incar.chuxing.api.UrlContainer;
import com.incar.chuxing.application.MyApplication;
import com.incar.chuxing.base.BaseActivity;
import com.incar.chuxing.bean.InviteFriendsBean;
import com.incar.chuxing.utils.AppManager;
import com.incar.chuxing.utils.ImageLoader;
import com.incar.chuxing.utils.ToastUtils;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class InviteFriendActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    private Context mContext = this;
    private Button button_share_wechat;
    private Button button_share_wxcircle;
    private Button button_share_qq;
    private ImageView inviteIv;

    @Override
    public int getLayoutId() {
        return R.layout.activity_invite;
    }


    @Override
    public void initData() {
        super.initData();
        inviteFriends();
    }


    @Override
    public void initView() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppManager.getAppManager().finishActivity();
            }
        });
        inviteIv = (ImageView) findViewById(R.id.invite_iv);
        button_share_wechat = (Button) findViewById(R.id.button_share_wechat);
        button_share_wxcircle = (Button) findViewById(R.id.button_share_wxcircle);
        button_share_qq = (Button) findViewById(R.id.button_share_qq);
        button_share_wechat.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                inviteFriendsShare(SHARE_MEDIA.WEIXIN);
            }
        });
        button_share_wxcircle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                inviteFriendsShare(SHARE_MEDIA.WEIXIN_CIRCLE);
            }
        });
        button_share_qq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                inviteFriendsShare(SHARE_MEDIA.QQ);
            }
        });
    }


    private void inviteFriends() {
        ApiStore.getInstance().getApiService(UrlContainer.Request.BASE_GATE_URL).inviteFriends(((MyApplication) getApplication()).getRequestFieldMap(), "1").enqueue(new Callback<InviteFriendsBean>() {
            @Override
            public void onResponse(Call<InviteFriendsBean> call, Response<InviteFriendsBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        String ivUrl = ((MyApplication) getApplication()).getImageUrl() + response.body().getAttachment().getInvitePicture();
                        ImageLoader.getInstance().displayImage(getApplicationContext(), ivUrl, inviteIv);
                    } else {
                        ToastUtils.makeText(mContext, response.body().getMessage(), ToastUtils.LENGTH_SHORT).show();
                    }
                } else {
                    ToastUtils.makeText(mContext, getResources().getString(R.string.serverError), ToastUtils.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<InviteFriendsBean> call, Throwable t) {
                ToastUtils.makeText(mContext, getResources().getString(R.string.badNetwork), ToastUtils.LENGTH_SHORT).show();
            }
        });
    }

    private void inviteFriendsShare(final SHARE_MEDIA platform) {
        ApiStore.getInstance().getApiService(UrlContainer.Request.BASE_GATE_URL).inviteFriends(((MyApplication) getApplication()).getRequestFieldMap(), "1").enqueue(new Callback<InviteFriendsBean>() {
            @Override
            public void onResponse(Call<InviteFriendsBean> call, Response<InviteFriendsBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        UMWeb web = new UMWeb(response.body().getAttachment().getUrl() + "?uid=" + response.body().getAttachment().getSessionUser().getUid());
                        web.setTitle(response.body().getAttachment().getTitle());
                        web.setDescription(response.body().getAttachment().getDescriptions());
                        web.setThumb(new UMImage(mContext, ((MyApplication) getApplication()).getImageUrl() + response.body().getAttachment().getPicture()));
                        new ShareAction(InviteFriendActivity.this).setPlatform(platform)
                                .withMedia(web)
                                .setCallback(umShareListener)
                                .share();
                    } else {
                        ToastUtils.makeText(mContext, response.body().getMessage(), ToastUtils.LENGTH_SHORT).show();
                    }
                } else {
                    ToastUtils.makeText(mContext, getResources().getString(R.string.serverError), ToastUtils.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<InviteFriendsBean> call, Throwable t) {
                ToastUtils.makeText(mContext, getResources().getString(R.string.badNetwork), ToastUtils.LENGTH_SHORT).show();
            }
        });
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //ToastUtils.makeText(mContext, "分享成功啦", ToastUtils.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //ToastUtils.makeText(mContext, "分享失败啦", ToastUtils.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //ToastUtils.makeText(mContext, "分享取消了", ToastUtils.LENGTH_SHORT).show();
                }
            });
        }
    };
}
